/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.controler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import samoloty.data.DBSingleton;
import samoloty.model.IDataModel;
import samoloty.model.models.ConstModel;
import samoloty.model.models.LimitLawModel;
import samoloty.model.models.LimitTowModel;
import samoloty.model.models.LimitZfwModel;
import samoloty.model.models.PaxWeightsModel;
import samoloty.model.models.StabTrimModel;
import samoloty.view.InfoSubPanel;

/**
 *
 * @author borysfan
 */
public class InfoPanelControler {

    private InfoSubPanel panel;
    private Controler controler;
    private IDataModel dataModel;
    private double vlizfwaft;
    private double vlizfwfwd;
    private double vlitowaft;
    private double vlitowfwd;
    private double vlilawaft;
    private double vlilawfwd;
    private int computation = 1;
    private int vmlaw;
    private int vmtow;

    public InfoPanelControler(Controler c) {
        controler = c;
        initPanel();
    }

    public InfoPanelControler(Controler c, IDataModel data) {
        dataModel = data;
        controler = c;
        initPanel();
        try {
            vmlaw = dataModel.getMaxWeights().getLanding();
            vmtow = dataModel.getMaxWeights().getTakeOff();
        } catch (NullPointerException ex) {
        }
    }

    private void initPanel() {
        panel = new InfoSubPanel();
    }

    public InfoSubPanel getPanel() {
        return panel;
    }

    public void update() {
        reset();
        double vazfw = compute_azfw();
        double vatow = compute_vatow();
        double valaw = compute_valaw();

        double vzfwi = compute_vzfwi();
        double vtowi = compute_vtowi();
        double vlawi = compute_vlawi();


        double vul = compute_vul();
        if (dataModel != null) {
            System.out.println("DataModel.class" + dataModel.getClass());

            int vmzfw = dataModel.getMaxWeights().getZeroFuel();
            set_vmzfw(vmzfw);
            set_vmtow(vmtow);
            set_vmlaw(vmlaw);

            compute_vlilawaft();
            compute_vlitowaft();
            compute_vlizfwaft();
            double vttlholdsw = compute_vttlholdsw();
            set_vttlholdsw(vttlholdsw);



            setFlaps1Lab();
            setFlaps2Lab();
            setFlaps3Lab();
            setFlaps4Lab();

        }
        double vmaclaw = compute_vmaclaw();
        double vmactow = compute_vmactow();
        double vmaczfw = compute_vmaczfw();
        set_vmaclaw(vmaclaw);
        set_vmactow(vmactow);
        set_vmaczfw(vmaczfw);

        set_vazfw(vazfw);
        set_vatow(vatow);
        set_valaw(valaw);

        set_vzfwi(vzfwi);
        set_vtowi(vtowi);
        set_vlawi(vlawi);


        set_vul(vul);
        controler.getPlotPanelControler().update();
    }

    public void reset() {
        panel.reset();
    }

    public double compute_vzfwi() {
        double vttlpaxw = compute_vttlpaxw();
        double vttlpax1 = compute_vttlpax1();
        double result = 0;
        try {
            PaxPanelControler paxControler = controler.getPaxPanelControler();

            double vdoi = compute_vdoi();
            double vpax0a = Double.parseDouble(paxControler.get_vpax0a());
            double vpax0aindex = dataModel.getPax().get(0).getIndex();
            double vpax0b = Double.parseDouble(paxControler.get_vpax0b());
            double vpax0bindex = dataModel.getPax().get(1).getIndex();
            double vpax0c = Double.parseDouble(paxControler.get_vpax0c());
            double vpax0cindex = dataModel.getPax().get(2).getIndex();

            HoldsPanelControler holdsControler = controler.getHoldsPanelControler();
            double vttlhold1 = (double) holdsControler.getHoldsPanelItemNo1().getTotal();
            double holdsIndex1 = holdsControler.getDataHoldsIndex("1");
            double vttlhold2 = (double) holdsControler.getHoldsPanelItemNo2().getTotal();
            double holdsIndex2 = holdsControler.getDataHoldsIndex("2");
            double vttlhold3 = (double) holdsControler.getHoldsPanelItemNo1().getTotal();
            double holdsIndex3 = holdsControler.getDataHoldsIndex("3");
            double vttlhold4 = (double) holdsControler.getHoldsPanelItemNo4().getTotal();
            double holdsIndex4 = holdsControler.getDataHoldsIndex("4");
            double vttlhold5 = (double) holdsControler.getHoldsPanelItemNo5().getTotal();
            double holdsIndex5 = holdsControler.getDataHoldsIndex("5");
            double vttlhold5l = (double) holdsControler.getHoldsPanelItemNo5l().getTotal();
            double holdsIndex5l = holdsControler.getDataHoldsIndex("5L");
            double vttlhold5r = (double) holdsControler.getHoldsPanelItemNo5r().getTotal();
            double holdsIndex5r = holdsControler.getDataHoldsIndex("5R");
            double vttlhold6 = (double) holdsControler.getHoldsPanelItemNo6().getTotal();
            double holdsIndex6 = holdsControler.getDataHoldsIndex("6");

            double vttlpaxwDivVttlpax1 = 0;

            if (isZero(vttlpax1) == false) {
                vttlpaxwDivVttlpax1 = vttlpaxw / vttlpax1;
            }

            result = vdoi + vpax0a * vpax0aindex * vttlpaxwDivVttlpax1 + vpax0b * vpax0bindex * vttlpaxwDivVttlpax1 + vpax0c * vpax0cindex * vttlpaxwDivVttlpax1;
            result += vttlhold1 * holdsIndex1 + vttlhold2 * holdsIndex2 + vttlhold3 * holdsIndex3 + vttlhold4 * holdsIndex4 + vttlhold5 * holdsIndex5 + vttlhold5l * holdsIndex5l + vttlhold5r * holdsIndex5r + vttlhold6 * holdsIndex6;

        } catch (NumberFormatException e) {
            System.err.println("InfoPanelControler. get_vzfwi. Bledny format");
        } catch (NullPointerException np) {
            System.err.println("InfoPanelControler. get_vzfwi. NullPointerException format");
        } catch (Exception e) {
            System.out.println("/t/t/t WYjatek");
        }
        result = roundToDecimals(result, 3);//zaokr do 3 m. po przecinku
        //result = Math.round(result);//zaokr do najblizszej calk
        return result;
    }

    public double compute_vtowi() {
        //vtowi=vzfwi+(znajdź wartość vtof w tabeli FUEL w kolumnie WEIGHT i odczytaj dla niej INDEX, zastosuj do tego interpolację dwóch najbliższych zmiennych, zarówno przy szukaniu WEIGHT jak i odczytywaniu INDEX)
        double result = 0;
        int vzfwi = (int) compute_vzfwi();

        FuelPanelControler fuelControler = controler.getFuelPanelControler();
        fuelControler.setDataModel(controler.getDataModel());
        int vtof = Integer.parseInt(fuelControler.get_vtof());

        result = vzfwi + fuelControler.getIndexFuelWeightApprox(vtof);

        result = roundToDecimals(result, 3);//zaokr do 3 m. po przecinku
        //result = Math.round(result);//zaokr do najblizszej calk
        //System.out.print("vtowi: " + result + "\n");
        return result;
    }

    public double compute_vlawi() {
        //vlawi=zfwi+(znajdź wartość (vtof-vtf) w tabeli FUEL w kolumnie WEIGHT i odczytaj dla niej INDEX, zastosuj do tego interpolację dwóch najbliższych zmiennych, zarówno przy szukaniu WEIGHT jak i odczytywaniu INDEX)
        double result = 0;
        int vzfwi = (int) compute_vzfwi();

        FuelPanelControler fuelControler = controler.getFuelPanelControler();
        fuelControler.setDataModel(controler.getDataModel());
        int vtof = Integer.parseInt(fuelControler.get_vtof());
        int vtf = Integer.parseInt(fuelControler.get_vtf());

        result = vzfwi + fuelControler.getIndexFuelWeightApprox(vtof - vtf);

        result = roundToDecimals(result, 3);//zaokr do 3 m. po przecinku
        //result = Math.round(result);//zaokr do najblizszej calk
        //System.out.print("vlawi: " + result + "\n");
        return result;
    }

    //vawfto-vdow-vtof-vttl=vul
    public int compute_vul() {
        int result = 0;
        try {
            int vawfto = compute_vawfto();
            System.out.println("Obliczenie 3,vafto: " + vawfto);
            int vdow = (int) compute_vdow();
            FuelPanelControler fuelControler = controler.getFuelPanelControler();
            int vtof = Integer.parseInt(fuelControler.get_vtof());
            int vttl = (int) compute_vttl();
            result = vawfto - vdow - vtof - vttl;
        } catch (NumberFormatException e) {
            System.err.println("InfoPanelControler. compute_vul. Bledny format");
        }
        System.out.println("Obliczenie 3 " + result);
        if (result < 0) {
            panel.hilight_vul();
        } else {
            panel.unhilight_vul();
        }
        return result;
    }

    public double compute_vttlpaxw() {
        double result = 0;

        PaxPanelControler paxControler = controler.getPaxPanelControler();
        PaxWeightsModel pax = paxControler.get_vpaxweights();
        if (pax != null) {

            int paxm = Integer.parseInt(paxControler.get_vpaxm());
            int paxf = Integer.parseInt(paxControler.get_vpaxf());
            int paxch = Integer.parseInt(paxControler.get_vpaxch());
            result += pax.getCh() * paxch + pax.getF() * paxf + pax.getM() * paxm;

        }
        return result;
    }

    public double compute_vttlpax1() {
        double result = 0;

        PaxPanelControler paxControler = controler.getPaxPanelControler();
        PaxWeightsModel pax = paxControler.get_vpaxweights();
        if (pax != null) {

            int paxm = Integer.parseInt(paxControler.get_vpaxm());
            int paxf = Integer.parseInt(paxControler.get_vpaxf());
            int paxch = Integer.parseInt(paxControler.get_vpaxch());
            result += paxch + paxf + paxm;

        }
        return result;
    }

    public double compute_vdow() {
        double result = 0;
        try {
            DowPanelControler dowControler = controler.getDowPanelControler();
            double vbasicweight = Double.parseDouble(dowControler.get_vbasicweight());
            double vcrewweight = dowControler.get_vcrew().getWeight();
            double vpantryfwd = Double.parseDouble(dowControler.get_vpantryfwd());
            double vpantrymid = Double.parseDouble(dowControler.get_vpantrymid());
            double vpantryaft = Double.parseDouble(dowControler.get_vpantryaft());
            double vadj1weight = Double.parseDouble(dowControler.geTvadj1weight());
            double vadj2weight = Double.parseDouble(dowControler.geTvadj2weight());
            double vadj3weight = Double.parseDouble(dowControler.geTvadj3weight());
            result = vbasicweight + vcrewweight + vpantryfwd + vpantrymid + vpantryaft + vadj1weight + vadj2weight + vadj3weight;
        } catch (NumberFormatException e) {
            System.err.println("InfoPanelControler. compute_vdow Bledny format liczb");
        } catch (NullPointerException e) {
            System.err.println("InfoPanelControler. NULL");
        }
        return result;
    }

    public int get_vmzfw() {
        return dataModel.getMaxWeights().getZeroFuel();
    }

    public int get_vmtow() {
        return vmtow;
        //return dataModel.getMaxWeights().getTakeOff();
    }

    public int get_vmlaw() {
        return vmlaw;
        //return dataModel.getMaxWeights().getLanding();
    }

    public int compute_vawfto() {
        //zapisz do zmiennej vawfto najmniejsza wartość z pośród trzech, gdzie druga też jest wyborem najmniejszej wartości z pośród dwóch: "vmzfw+vtof"; (mniejsza z pośród: "vmtow"; "tablica MAX WEIGHTS wartość RAMP/TAXI"-"vtxf"); "vmlaw+vtf"
        int result = Integer.MAX_VALUE;
        try {

            int vmzfw = dataModel.getMaxWeights().getZeroFuel();//max zfw z info

            int c_vmtow = Integer.parseInt(controler.getDowPanelControler().get_vmtow());
            int d_vmtow = dataModel.getMaxWeights().getTakeOff();//max tow z info
            int c_vmlaw = Integer.parseInt(controler.getDowPanelControler().get_vmlaw());
            int d_vmlaw = dataModel.getMaxWeights().getLanding();// max law z info
            vmtow = d_vmtow;
            vmlaw = d_vmlaw;
            if (c_vmtow < d_vmtow) {
                vmtow = c_vmtow;
            }
            if (c_vmlaw < d_vmlaw) {
                vmlaw = c_vmlaw;
            }
            int ramptaxi = dataModel.getMaxWeights().getRampTaxi();

            FuelPanelControler fuelControler = controler.getFuelPanelControler();
            int vtof = Integer.parseInt(fuelControler.get_vtof());
            int vtxf = Integer.parseInt(fuelControler.get_vtxf());
            int vtf = Integer.parseInt(fuelControler.get_vtf());

            int first = vmzfw + vtof;
            System.out.print("Obliczenie 2a:" + first + "\n");
            int second = Math.min(vmtow, ramptaxi - vtxf);
            System.out.print("Obliczenie 2b:" + second + "\n");
            int third = vmlaw + vtf;
            System.out.print("Obliczenie 2c:" + third + "\n");
            if (result > first) {
                result = first;
                computation = 1;
            }
            if (result > second) {
                result = second;
                computation = 2;
            }
            if (result > third) {
                result = third;
                computation = 3;
            }

        } catch (NumberFormatException e) {
            System.err.println("InfoPanelControler. get_vawfto. Bledny format");
        } catch (NullPointerException np) {
        }
        return result;
    }

    public double compute_vttlholdsw() {
        double result = 0;
        HoldsPanelControler holdsControler = controler.getHoldsPanelControler();
        double vttlhold1 = holdsControler.getHoldsPanelItemNo1().getTotal();
        double vttlhold2 = holdsControler.getHoldsPanelItemNo2().getTotal();
        double vttlhold3 = holdsControler.getHoldsPanelItemNo3().getTotal();
        double vttlhold4 = holdsControler.getHoldsPanelItemNo4().getTotal();
        double vttlhold5 = holdsControler.getHoldsPanelItemNo5().getTotal();
        double vttlhold5l = holdsControler.getHoldsPanelItemNo5l().getTotal();
        double vttlhold5r = holdsControler.getHoldsPanelItemNo5r().getTotal();
        double vttlhold6 = holdsControler.getHoldsPanelItemNo6().getTotal();
        result = vttlhold1 + vttlhold2 + vttlhold3 + vttlhold4 + vttlhold5 + vttlhold5l + vttlhold5r + vttlhold6;
        return result;
    }

    public double compute_vttl() {
        double result = 0;
        double vttlholdsw = compute_vttlholdsw();
        double vttlPaxw = compute_vttlpaxw(); //OBLICZYC vttlPaxw nie wiem skad wziac // schemat.xls/pax/ostatni_wiersz?
        result = vttlPaxw + vttlholdsw;
        return result;
    }

    public double compute_azfw() {
        if (dataModel != null) {
            double result = 0;
            double vdow = compute_vdow();//nie wiem skad wziac v dow
            double vttl = compute_vttl();
            result = vdow + vttl;
            int vmzfw = dataModel.getMaxWeights().getZeroFuel();
            if (result > vmzfw) {
                panel.hilight_vazfw();
            } else {
                panel.unhilight_vazfw();
            }
            return result;
        }
        return 0;
    }

    public double compute_vatow() {
        if (dataModel != null) {
            double result = 0;
            try {
                double vazfw = compute_azfw();
                FuelPanelControler fuelControler = controler.getFuelPanelControler();
                double vtof = Double.parseDouble(fuelControler.get_vtof());
                result = vazfw + vtof;
            } catch (NumberFormatException e) {
                System.err.println("InfoPanelControler. get_vatow. Bledny format");
            }
            if (result > vmtow) {
                panel.hilight_vatow();
            } else {
                panel.unhilight_vatow();
            }
            return result;
        }
        return 0;
    }

    public double compute_valaw() {
        double result = 0;
        if (dataModel != null) {
            try {
                double vatow = compute_vatow();
                FuelPanelControler fuelControler = controler.getFuelPanelControler();
                double vtf = Double.parseDouble(fuelControler.get_vtf());
                result = vatow - vtf;
            } catch (NumberFormatException e) {
                System.err.println("InfoPanelControler. get_valaw. Bledny format");
            }
            if (result > vmlaw) {
                panel.hilight_valaw();
            } else {
                panel.unhilight_valaw();
            }
            return result;
        }
        return 0;
    }

    public double compute_vdoi() {
        //vdoi=vbasicindex+vcrewindex+vpantryfwd*(tablica PANTRY wartość INDEX_FWD)+vpantrymid*(tablica PANTRY wartość INDEX_MID)+vpantryaft*(tablica PANTRY wartość INDEX_AFT)
        double result = 0;
        try {
            DowPanelControler dowControler = controler.getDowPanelControler();
            double vbasicindex = Double.parseDouble(dowControler.get_vbasicindex());
            double vcrewindex = dowControler.get_vcrew().getIndex();
            double vpantryfwd = Double.parseDouble(dowControler.get_vpantryfwd());
            double fwdIndex = dataModel.getPantry().getFwdIndex();
            double vpantrymid = Double.parseDouble(dowControler.get_vpantrymid());
            double midIndex = dataModel.getPantry().getMidIndex();
            double vpantryaft = Double.parseDouble(dowControler.get_vpantryaft());
            double aftIndex = dataModel.getPantry().getAftIndex();
            double vadj1index = Double.parseDouble(dowControler.geTvadj1index());
            double vadj2index = Double.parseDouble(dowControler.geTvadj2index());
            double vadj3index = Double.parseDouble(dowControler.geTvadj3index());
            result = vbasicindex + vcrewindex + vpantryfwd * fwdIndex + vpantrymid * midIndex + vpantryaft * aftIndex + vadj1index + vadj2index + vadj3index;
        } catch (NumberFormatException e) {
            System.err.println("InfoPanelControler. get_vdoi. Bledny format. " + e.getLocalizedMessage());
        } catch (NullPointerException np) {
            System.err.println("InfoPanelControler. get_vdoi. Bledny format. NullPointerException np");
        }

        /*result = roundToDecimals(result, 3);//zaokr do 3 m. po przecinku
        result = Math.round(result);//zaokr do najblizszej calk*/
        return result;
    }

    public double compute_vmaczfw() {
        //14//vmaczfw=((C*(vzfwi - K)/vazfw) + REFSTA - LEMAC)/(MAC/100)
        if (dataModel != null) {
            ConstModel aCon = dataModel.getConst();
            double vazfw = compute_vdow() + compute_vttl();
            double vzfwi = compute_vzfwi();
            return ((aCon.getC() * (vzfwi - aCon.getK()) / vazfw) + aCon.getRefsta() - aCon.getLemac()) / (aCon.getMac() / 100);
        }
        return 0;
    }

    private void set_vmaclaw(double vmaclaw) {
        panel.set_vmaclaw(roundDouble0(vmaclaw));
    }

    private void set_vmactow(double vmactow) {
        panel.set_vmactow(roundDouble0(vmactow));
    }

    private void set_vmaczfw(double vmaczfw) {
        panel.set_vmaczfw(roundDouble0(vmaczfw));
    }

    public double compute_vmactow() {
        //15//vmactow=((C*(vtowi - K)/vatow) + REFSTA - LEMAC)/(MAC/100)
        if (dataModel != null) {
            ConstModel aCon = dataModel.getConst();
            double vtowi = compute_vtowi();
            double vatow = compute_vatow();
            //System.out.print("Obliczenia 15: " + ((aCon.getC() * (vtowi - aCon.getK()) / vatow) + aCon.getRefsta() - aCon.getLemac()) / (aCon.getMac() / 100) + "\n");
            return ((aCon.getC() * (vtowi - aCon.getK()) / vatow) + aCon.getRefsta() - aCon.getLemac()) / (aCon.getMac() / 100);
        }
        return 0;
    }

    public double compute_vmaclaw() {
        //16//vmaclaw=((C*(vlawi - K)/valaw) + REFSTA - LEMAC)/(MAC/100)
        if (dataModel != null) {
            ConstModel aCon = dataModel.getConst();
            double vlawi = compute_vlawi();
            double valaw = compute_valaw();
            //System.out.print("Obliczenia 16: " + ((aCon.getC() * (vlawi - aCon.getK()) / valaw) + aCon.getRefsta() - aCon.getLemac()) / (aCon.getMac() / 100) + "\n");
            return ((aCon.getC() * (vlawi - aCon.getK()) / valaw) + aCon.getRefsta() - aCon.getLemac()) / (aCon.getMac() / 100);
        }
        return 0;
    }

    public double compute_vlizfwaft() {
        try {
            if (dataModel != null) {
                ArrayList<LimitZfwModel> limitZfw = dataModel.getLimitZfw();
                double vazfw = compute_azfw();
                boolean highest = false;
                double LW_ZFW_FWD2, LW_ZFW_FWD1;
                double LI_ZFW_FWD1, LI_ZFW_FWD2;
                int i;
                for (i = 0; i < limitZfw.size(); i++) {
                    int weightFwd = limitZfw.get(i).getWeightFwd();
                    if ((double) weightFwd > vazfw) {
                        highest = true;
                        break;
                    }
                }
                if (highest) {
                    LW_ZFW_FWD2 = limitZfw.get(i).getWeightFwd();
                    LI_ZFW_FWD2 = limitZfw.get(i).getIndexFwd();
                    LW_ZFW_FWD1 = limitZfw.get(i - 1).getWeightFwd();
                    LI_ZFW_FWD1 = limitZfw.get(i - 1).getIndexFwd();
                } else {
                    LW_ZFW_FWD1 = LW_ZFW_FWD2 = limitZfw.get(i).getWeightFwd();
                    LI_ZFW_FWD1 = LI_ZFW_FWD2 = limitZfw.get(i).getIndexFwd();
                }
                this.vlizfwfwd = (LI_ZFW_FWD2 - LI_ZFW_FWD1) * (vazfw - LW_ZFW_FWD1) / (LW_ZFW_FWD2 - LW_ZFW_FWD1) + LI_ZFW_FWD1;
                System.err.println("\t\tvli zfw fwd:" + vlizfwfwd);

                boolean highest2 = false;
                double LW_ZFW_AFT2, LW_ZFW_AFT1;
                double LI_ZFW_AFT1, LI_ZFW_AFT2;
                int j;
                for (j = 0; j < limitZfw.size(); j++) {
                    int weightAFT = limitZfw.get(j).getWeightAft();
                    if ((double) weightAFT > vazfw) {
                        highest2 = true;
                        break;
                    }
                }
                if (highest2) {
                    LW_ZFW_AFT2 = limitZfw.get(j).getWeightAft();
                    LI_ZFW_AFT2 = limitZfw.get(j).getIndexAft();
                    LW_ZFW_AFT1 = limitZfw.get(j - 1).getWeightAft();
                    LI_ZFW_AFT1 = limitZfw.get(j - 1).getIndexAft();
                } else {
                    LW_ZFW_AFT1 = LW_ZFW_AFT2 = limitZfw.get(j).getWeightAft();
                    LI_ZFW_AFT1 = LI_ZFW_AFT2 = limitZfw.get(j).getIndexAft();
                }
                this.vlizfwaft = (LI_ZFW_AFT2 - LI_ZFW_AFT1) * (vazfw - LW_ZFW_AFT1) / (LW_ZFW_AFT2 - LW_ZFW_AFT1) + LI_ZFW_AFT1;
                System.err.println("\t\tvli zfw aft:" + vlizfwaft);
                int vzfwi = (int) compute_vzfwi();
                if (vlizfwfwd <= vzfwi && vzfwi <= vlizfwaft) {
                    panel.hilight_vzfwi();
                } else {
                    panel.unhilight_vzfwi();
                }
            } else {
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("vli zfw aioobe");
        } catch (IndexOutOfBoundsException ex) {
            System.err.println("vli zfw ioobe");
        }
        return vlizfwaft;
    }

    public double compute_vlizfwfwd() {
        compute_vlizfwaft();
        return this.vlizfwfwd;
    }

    public double compute_vlitowaft() {
        try {
            if (dataModel != null) {
                ArrayList<LimitTowModel> limitTow = dataModel.getLimitTow();
                double vatow = compute_vatow();
                boolean highest = false;
                double LW_ZFW_FWD2, LW_ZFW_FWD1;
                double LI_ZFW_FWD1, LI_ZFW_FWD2;
                int i;
                for (i = 0; i < limitTow.size(); i++) {
                    int weightFwd = limitTow.get(i).getWeightFwd();
                    if ((double) weightFwd > vatow) {
                        highest = true;
                        break;
                    }
                }
                if (highest) {
                    LW_ZFW_FWD2 = limitTow.get(i).getWeightFwd();
                    LI_ZFW_FWD2 = limitTow.get(i).getIndexFwd();
                    LW_ZFW_FWD1 = limitTow.get(i - 1).getWeightFwd();
                    LI_ZFW_FWD1 = limitTow.get(i - 1).getIndexFwd();
                } else {
                    LW_ZFW_FWD1 = LW_ZFW_FWD2 = limitTow.get(i).getWeightFwd();
                    LI_ZFW_FWD1 = LI_ZFW_FWD2 = limitTow.get(i).getIndexFwd();
                }
                this.vlitowfwd = (LI_ZFW_FWD2 - LI_ZFW_FWD1) * (vatow - LW_ZFW_FWD1) / (LW_ZFW_FWD2 - LW_ZFW_FWD1) + LI_ZFW_FWD1;
                System.err.println("\t\tvli tow fwd:" + vlitowfwd);

                boolean highest2 = false;
                double LW_ZFW_AFT2, LW_ZFW_AFT1;
                double LI_ZFW_AFT1, LI_ZFW_AFT2;
                int j;
                for (j = 0; j < limitTow.size(); j++) {
                    int weightAFT = limitTow.get(j).getWeightAft();
                    if ((double) weightAFT > vatow) {
                        highest2 = true;
                        break;
                    }
                }
                if (highest2) {
                    LW_ZFW_AFT2 = limitTow.get(j).getWeightAft();
                    LI_ZFW_AFT2 = limitTow.get(j).getIndexAft();
                    LW_ZFW_AFT1 = limitTow.get(j - 1).getWeightAft();
                    LI_ZFW_AFT1 = limitTow.get(j - 1).getIndexAft();
                } else {
                    LW_ZFW_AFT1 = LW_ZFW_AFT2 = limitTow.get(j).getWeightAft();
                    LI_ZFW_AFT1 = LI_ZFW_AFT2 = limitTow.get(j).getIndexAft();
                }
                this.vlitowaft = (LI_ZFW_AFT2 - LI_ZFW_AFT1) * (vatow - LW_ZFW_AFT1) / (LW_ZFW_AFT2 - LW_ZFW_AFT1) + LI_ZFW_AFT1;
                System.err.println("\t\tvli tow aft:" + vlitowaft);
                int vtowi = (int) compute_vtowi();
                if (vlitowfwd <= vtowi && vtowi <= vlitowaft) {
                    panel.hilight_vtowi();
                } else {
                    panel.unhilight_vtowi();
                }
            } else {
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("vli tow aioobe");
        } catch (IndexOutOfBoundsException ex) {
            System.err.println("vli tow ioobe");
        }
        return vlitowaft;
    }

    public double compute_vlilawaft() {
        try {
            if (dataModel != null) {
                ArrayList<LimitLawModel> limitLaw = dataModel.getLimitLaw();
                double valaw = compute_valaw();
                boolean highest = false;
                double LW_ZFW_FWD2, LW_ZFW_FWD1;
                double LI_ZFW_FWD1, LI_ZFW_FWD2;
                int i;
                for (i = 0; i < limitLaw.size(); i++) {
                    int weightFwd = limitLaw.get(i).getWeightFwd();
                    if ((double) weightFwd > valaw) {
                        highest = true;
                        break;
                    }
                }
                if (highest) {
                    LW_ZFW_FWD2 = limitLaw.get(i).getWeightFwd();
                    LI_ZFW_FWD2 = limitLaw.get(i).getIndexFwd();
                    LW_ZFW_FWD1 = limitLaw.get(i - 1).getWeightFwd();
                    LI_ZFW_FWD1 = limitLaw.get(i - 1).getIndexFwd();
                } else {
                    LW_ZFW_FWD1 = LW_ZFW_FWD2 = limitLaw.get(i).getWeightFwd();
                    LI_ZFW_FWD1 = LI_ZFW_FWD2 = limitLaw.get(i).getIndexFwd();
                }
                this.vlilawfwd = (LI_ZFW_FWD2 - LI_ZFW_FWD1) * (valaw - LW_ZFW_FWD1) / (LW_ZFW_FWD2 - LW_ZFW_FWD1) + LI_ZFW_FWD1;
                System.err.println("\t\tvli law fwd:" + vlilawfwd);

                boolean highest2 = false;
                double LW_ZFW_AFT2, LW_ZFW_AFT1;
                double LI_ZFW_AFT1, LI_ZFW_AFT2;
                int j;
                for (j = 0; j < limitLaw.size(); j++) {
                    int weightAFT = limitLaw.get(j).getWeightAft();
                    if ((double) weightAFT > valaw) {
                        highest2 = true;
                        break;
                    }
                }
                if (highest2) {
                    LW_ZFW_AFT2 = limitLaw.get(j).getWeightAft();
                    LI_ZFW_AFT2 = limitLaw.get(j).getIndexAft();
                    LW_ZFW_AFT1 = limitLaw.get(j - 1).getWeightAft();
                    LI_ZFW_AFT1 = limitLaw.get(j - 1).getIndexAft();
                } else {
                    LW_ZFW_AFT1 = LW_ZFW_AFT2 = limitLaw.get(j).getWeightAft();
                    LI_ZFW_AFT1 = LI_ZFW_AFT2 = limitLaw.get(j).getIndexAft();
                }
                this.vlilawaft = (LI_ZFW_AFT2 - LI_ZFW_AFT1) * (valaw - LW_ZFW_AFT1) / (LW_ZFW_AFT2 - LW_ZFW_AFT1) + LI_ZFW_AFT1;
                System.err.println("\t\tvli law aft:" + vlilawaft);
                int vlawi = (int) compute_vlawi();
                if (vlilawfwd <= vlawi && vlawi <= vlilawaft) {
                    panel.hilight_vlawi();
                } else {
                    panel.unhilight_vlawi();
                }
            } else {
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("vli law aioobe");
        } catch (IndexOutOfBoundsException ex) {
            System.err.println("vli law ioobe");
        } catch (NullPointerException exc) {
            System.err.println("vli law npe");
        }
        return vlilawaft;
    }
    /*
    public int get_vmzfw() {
    int size = dataModel.getLimitZfw().size();
    if (size > 0) {
    return dataModel.getLimitZfw().get(size-1).getWeightAft();
    } else {
    return 0;
    }
    }

    public int get_vmtow() {
    int size = dataModel.getLimitTow().size();
    if (size > 0) {
    return dataModel.getLimitTow().get(size-1).getWeightAft();
    } else {
    return 0;
    }
    }

    public int get_vmlaw() {
    int size = dataModel.getLimitLaw().size();
    if (size > 0) {
    return dataModel.getLimitLaw().get(size-1).getWeightAft();
    } else {
    return 0;
    }
    }*/

    public int getComputation() {
        compute_vawfto();
        compute_vul();
        return computation;
    }

    /*FUNKCJE USTAWIAJACE WARTOSCI W INFO PANELU*/
    public void set_vazfw(double value) {
        panel.set_vazfw(value);
    }

    public void set_vatow(double value) {
        panel.set_vatow(value);
    }

    public void set_valaw(double value) {
        panel.set_valaw(value);
    }

    public void set_vmzfw(double value) {
        panel.set_vmzfw(value);
    }

    public void set_vmtow(double value) {
        panel.set_vmtow(value);
    }

    public void set_vmlaw(double value) {
        panel.set_vmlaw(value);
    }

    public void set_vul(double value) {
        panel.set_vul(value);
    }

    public void set_vzfwi(double value) {
        panel.set_vzfwi(value);
    }

    public void set_vtowi(double value) {

        panel.set_vtowi(value);
    }

    public void set_vlawi(double value) {
        panel.set_vlawi(value);
    }

    void setDataModel(IDataModel data) {
        dataModel = data;
    }

    public void setFlaps1Lab() {//label
        if (dataModel != null) {
            StabTrimModel stabTrimFlap1 = dataModel.getStabTrimFlap1();
            if (stabTrimFlap1 != null) {
                String display = stabTrimFlap1.getStabTrimSubModel().get(0).getDisplay();
                panel.setFlaps1Lab(display);
                setStabTrim1Lab();
                System.out.println("SetFlaps1Lab:" + display);
            } else {
                panel.setFlaps1Lab("");
                panel.setStabTrim1Lab("");
            }
        }
    }

    public void setFlaps2Lab() {//label
        if (dataModel != null) {
            StabTrimModel stabTrimFlap2 = dataModel.getStabTrimFlap2();
            if (stabTrimFlap2 != null) {
                String display = stabTrimFlap2.getStabTrimSubModel().get(0).getDisplay();
                panel.setFlaps2Lab(display);
                setStabTrim2Lab();
                System.out.println("SetFlaps2Lab:" + display);
            } else {
                panel.setFlaps2Lab("");
                panel.setStabTrim2Lab("");
            }
        }
    }

    public void setFlaps3Lab() {//label
        if (dataModel != null) {
            StabTrimModel stabTrimFlap3 = dataModel.getStabTrimFlap3();
            if (stabTrimFlap3 != null) {
                String display = stabTrimFlap3.getStabTrimSubModel().get(0).getDisplay();
                panel.setFlaps3Lab(display);
                setStabTrim3Lab();
                System.out.println("SetFlaps3Lab:" + display);
            } else {
                panel.setFlaps3Lab("");
                panel.setStabTrim3Lab("");
            }
        }
    }

    public void setFlaps4Lab() {//label
        if (dataModel != null) {
            StabTrimModel stabTrimFlap4 = dataModel.getStabTrimFlap4();
            if (stabTrimFlap4 != null) {
                String display = stabTrimFlap4.getStabTrimSubModel().get(0).getDisplay();
                panel.setFlaps4Lab(display);
                setStabTrim4Lab();
                System.out.println("SetFlaps4Lab:" + display);
            } else {
                panel.setFlaps4Lab("");
                panel.setStabTrim4Lab("");
            }
        }
    }

    public double setStabTrim1Lab() {//wartosc
        double vatow = compute_vatow();
        double vmactow = compute_vmactow();
        try {
            double findStab = dataModel.getStabTrimFlap1().findStab((int) vatow, vmactow);
            System.out.print("\t\t StabTrim1: " + findStab + "\n");
            panel.setStabTrim1Lab(roundDouble(findStab));
            return findStab;
        } catch (IndexOutOfBoundsException e) {
        } catch (NullPointerException e) {
        }
        return 0;
    }

    public double setStabTrim2Lab() {//wartosc
        double vatow = compute_vatow();
        double vmactow = compute_vmactow();
        try {
            double findStab = dataModel.getStabTrimFlap2().findStab((int) vatow, vmactow);
            System.out.print("\t\t StabTrim2: " + findStab + "\n");
            panel.setStabTrim2Lab(roundDouble(findStab));
            return findStab;
        } catch (IndexOutOfBoundsException e) {
        } catch (NullPointerException e) {
        }
        return 0;
    }

    public double setStabTrim3Lab() {//wartosc
        double vatow = compute_vatow();
        double vmactow = compute_vmactow();
        try {
            double findStab = dataModel.getStabTrimFlap3().findStab((int) vatow, vmactow);
            System.out.print("\t\t StabTrim2: " + findStab + "\n");
            panel.setStabTrim3Lab(roundDouble(findStab));
            return findStab;
        } catch (IndexOutOfBoundsException e) {
        } catch (NullPointerException e) {
        }
        return 0;
    }

    public double setStabTrim4Lab() {//wartosc
        double vatow = compute_vatow();
        double vmactow = compute_vmactow();
        try {
            double findStab = dataModel.getStabTrimFlap4().findStab((int) vatow, vmactow);
            System.out.print("\t\t StabTrim4: " + findStab + "\n");
            panel.setStabTrim4Lab(roundDouble(findStab));
            return findStab;
        } catch (IndexOutOfBoundsException e) {
        } catch (NullPointerException e) {
        }
        return 0;
    }

    public String roundDouble(double value) {
        DecimalFormat fmt = new DecimalFormat();
        fmt.setMaximumFractionDigits(2);
        return fmt.format(value);
    }

    public String roundDouble0(double value) {
        DecimalFormat fmt = new DecimalFormat();
        fmt.setMaximumFractionDigits(0);
        return fmt.format(value);
    }

    public double roundToDecimals(double d, int c) {
        int temp = (int) ((d * Math.pow(10, c)));
        return (((double) temp) / Math.pow(10, c));
    }

    public static boolean isZero(double value) {
        double eps = 10e-9;
        if (value > -eps && value < eps) {
            return true;
        }
        return false;
    }

    private void set_vttlholdsw(double vttlholdsw) {
        controler.getHoldsPanelControler().set_vttlholdsw(vttlholdsw);
    }


    public boolean isValid(){
        //TODO
        //return panel.isValid();//after testing remove line below, and uncoment this return
        return true;
    }
}
